/**
 *******************************************************************************************************************************************
 * @file        dev_touch.h
 * @brief       TP 触摸设备驱动层
 * @details     /
 * @since       Change Logs:
 * Date         Author       Notes
 * 2022-12-08   lzh          the first version
 * @remark      /
 *******************************************************************************************************************************************
 * @attention
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
 * REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
 * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
 * -ECTION WITH THEIR PRODUCTS.
 *
 * @copyright   2012 Synwit Technology
 *******************************************************************************************************************************************
 */
#ifndef __DEV_TOUCH_H__
#define __DEV_TOUCH_H__

#include "SWM341.h"
#include "version.h"

/*******************************************************************************************************************************************
 * Public Define
 *******************************************************************************************************************************************/
#if (SWM34S_LCM_PCBV == SWM34SCE_PIN48_A2)
#define TP_GPIO_RST         GPIOM
#define TP_PIN_RST          PIN4

#define TP_GPIO_INT         GPIOM
#define TP_PIN_INT          PIN5
#define TP_INT_IRQn         GPIOM_IRQn
#define TP_INT_Handler      GPIOM_Handler

#define TP_GPIO_SCL         GPIOM
#define TP_PIN_SCL          PIN2

#define TP_GPIO_SDA         GPIOM
#define TP_PIN_SDA          PIN3

/* 该端口无法复用为硬件 I2C, 只能用软件模拟, 故以下端口宏无实际作用, 仅为了应对编译时可能出现的错误 */
#define TP_PORT_SCL         PORTM
#define TP_PIN_SCL_FUN      PORTA_PIN1_I2C0_SCL

#define TP_PORT_SDA         PORTM
#define TP_PIN_SDA_FUN      PORTA_PIN0_I2C0_SDA

#define TP_I2C_X            I2C0

#elif (SWM34S_LCM_PCBV == SWM34SRE_PIN64_A001)
#define TP_GPIO_RST         GPIOC
#define TP_PIN_RST          PIN8

#define TP_GPIO_INT         GPIOD
#define TP_PIN_INT          PIN0
#define TP_INT_IRQn         GPIOD_IRQn
#define TP_INT_Handler      GPIOD_Handler

#define TP_GPIO_SCL         GPIOC
#define TP_PORT_SCL         PORTC
#define TP_PIN_SCL          PIN5
#define TP_PIN_SCL_FUN      PORTC_PIN5_I2C1_SCL

#define TP_GPIO_SDA         GPIOC
#define TP_PORT_SDA         PORTC
#define TP_PIN_SDA          PIN4
#define TP_PIN_SDA_FUN      PORTC_PIN4_I2C1_SDA

#define TP_I2C_X            I2C1

#else /* SWM34SVET6_Pin100 */
#define TP_GPIO_RST         GPIOA
#define TP_PIN_RST          PIN5

#define TP_GPIO_INT         GPIOD
#define TP_PIN_INT          PIN0
#define TP_INT_IRQn         GPIOD_IRQn
#define TP_INT_Handler      GPIOD_Handler

#if (SWM34S_LCM_PCBV == SWM34SVE_PIN100_A001)
#define TP_GPIO_SCL         GPIOA
#define TP_PORT_SCL         PORTA
#define TP_PIN_SCL          PIN7
#define TP_PIN_SCL_FUN      PORTA_PIN7_I2C1_SCL

#define TP_GPIO_SDA         GPIOA
#define TP_PORT_SDA         PORTA
#define TP_PIN_SDA          PIN6
#define TP_PIN_SDA_FUN      PORTA_PIN6_I2C1_SDA

#define TP_I2C_X            I2C1

#elif (SWM34S_LCM_PCBV == SWM34SVE_PIN100_A002)
#define TP_GPIO_SCL         GPIOA
#define TP_PORT_SCL         PORTA
#define TP_PIN_SCL          PIN1
#define TP_PIN_SCL_FUN      PORTA_PIN1_I2C0_SCL

#define TP_GPIO_SDA         GPIOA
#define TP_PORT_SDA         PORTA
#define TP_PIN_SDA          PIN0
#define TP_PIN_SDA_FUN      PORTA_PIN0_I2C0_SDA

#define TP_I2C_X            I2C0

#endif

#endif

/*******************************************************************************************************************************************
 * Public Typedef
 *******************************************************************************************************************************************/
/** 触摸事件枚举 */
typedef enum
{
    TP_EVENT_RELEASED = 0, /**< 触屏释放 */
    TP_EVENT_PRESSED       /**< 触屏按下 */
} tp_event_t;

/** 触摸数据结构体 */
typedef struct
{
    uint16_t x;         /**< 触摸点 X 轴坐标 */
    uint16_t y;         /**< 触摸点 Y 轴坐标 */
    tp_event_t event;   /**< 触摸事件(抬起/按下/移动等等) */
    uint8_t width;      /**< 触摸点的宽度大小(Reserve) */
    uint8_t track_id;   /**< 每个触摸点的触摸轨迹(Reserve) */
} tp_data_t;

/** 触摸屏种类枚举 */
typedef enum
{
    TP_TYPE_CT = 0, /**< 电容式触摸屏 */
    TP_TYPE_RT      /**< 电阻式触摸屏 */
} tp_type_t;

/** 触摸屏工作模式 */
typedef enum
{
    TP_MODE_POLLING_RDONLY = 0, /**< 轮询模式 */
    TP_MODE_INT_RX              /**< 中断接收模式(INT触发) */
} tp_mode_t;

/** 触摸屏设备类 */
typedef struct
{
    tp_data_t *point;          /**< 触摸点数据 */
    const uint8_t points_num;  /**< 触摸点上限 */
    const tp_type_t type;      /**< 触摸设备类型 */
    const tp_mode_t mode;      /**< 触摸设备工作模式 */

    /** (return: 0 - success    other - error_code)<触摸设备初始化>(None) */
    uint8_t (*const init_cb)(void);

    /** (返回读取到的触点信息的个数)<读取触摸点数据>(触摸点数据, 期望读取的触摸点数) */
    uint8_t (*const read_cb)(tp_data_t *p_data, uint8_t points);

    /** 关闭触摸设备(eg: 低功耗) */
    void (*const close_cb)(void);
} dev_tp_t;

/*******************************************************************************************************************************************
 * Public Variable
 *******************************************************************************************************************************************/
/** 触摸屏设备 */
extern dev_tp_t Dev_TP;

/*******************************************************************************************************************************************
 * Public Function
 *******************************************************************************************************************************************/
/**
 * @brief   初始化触摸设备驱动
 * @param   dev : 设备句柄
 * @retval  0     : success
 * @retval  other : error code
 */
uint8_t touch_init(dev_tp_t *dev);

/**
 * @brief   触摸事件处理
 * @param   dev : 设备句柄
 * @retval  /
 * @note    INT 中断触发->查询扫描
 */
void touch_handler(dev_tp_t *dev);

#endif //__DEV_TOUCH_H__
